<template>
  <div class="app-container">
    <el-row>
      <el-col :xs="24" class="table_big_bg">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
            <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
              <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                <el-form-item label="用户编号" prop="UserID">
                  <el-input
                    v-model="queryParams.UserID"
                    placeholder="请输入用户编号"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                  <el-input
                    v-model="queryParams.name"
                    placeholder="请输入姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item label="表号" prop="TerminalID">
                  <el-input
                    v-model="queryParams.TerminalID"
                    placeholder="请输入表号"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item style="width: 0px">
                </el-form-item>
              </div>
            </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
              <span>
                {{ fold ? '收起筛选' : '更多筛选' }}
              </span>
            </el-button>
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8 padding_top_bottom">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['datastatistics:deviceinstall:add']"
            >新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['datastatistics:deviceinstall:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['datastatistics:deviceinstall:remove']"
            >删除</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['datastatistics:deviceinstall:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="deviceinstallList" @selection-change="handleSelectionChange" max-height="600" ref="tableList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="用户编号" align="center" prop="UserID" v-if="columns[0].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="姓名" align="center" prop="name" v-if="columns[1].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="表号" align="center" prop="TerminalID" v-if="columns[2].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="电话号码" align="center" prop="Phone" v-if="columns[3].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="地址" align="center" prop="address" v-if="columns[4].visible" :show-overflow-tooltip="true" />
          <el-table-column label="备注" align="center" prop="remark" v-if="columns[5].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['datastatistics:deviceinstall:edit']"
              >修改</el-button>
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['datastatistics:deviceinstall:remove']"
              >删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改安装数据上传对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="用户编号" prop="UserID">
          <el-input v-model="form.UserID" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="表号" prop="TerminalID">
          <el-input v-model="form.TerminalID" placeholder="请输入表号" />
        </el-form-item>
        <el-form-item label="电话号码" prop="Phone">
          <el-input v-model="form.Phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceinstall, getDeviceinstall, delDeviceinstall, addDeviceinstall, updateDeviceinstall } from "@/api/dataquery/deviceinstallrecord";

export default {
  name: "deviceinstallrecord",
  data() {
    return {
      size:'small',
      collapseAble: false,
      fold: false,
      maxShow: '5',
      labelWidth: '90px',
      // 列信息
      columns: [
        // { key: 0, label: `自增主键`, visible: false },
        // { key: 1, label: `设备归属水司`, visible: true },
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `姓名`, visible: true },
        { key: 2, label: `表号`, visible: true },
        { key: 3, label: `电话号码`, visible: true },
        { key: 4, label: `地址`, visible: true },
        { key: 5, label: `备注`, visible: true },
        // { key: 7, label: `备注`, visible: true },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安装数据上传表格数据
      deviceinstallList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        UserID: null,
        name: null,
        TerminalID: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        TerminalID: [
          { required: true, message: "表号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    /** 查询安装数据上传列表 */
    getList() {
      this.loading = true;
      listDeviceinstall(this.queryParams).then(response => {
        this.deviceinstallList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        UserID: null,
        name: null,
        TerminalID: null,
        Phone: null,
        address: null,
        remark: null,
        id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备安装记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      debugger
      this.reset();
      const id = row.id || this.ids
      getDeviceinstall(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备安装记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceinstall(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceinstall(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除安装数据上传编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceinstall(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datastatistics/deviceinstall/export', {
        ...this.queryParams
      }, `设备安装记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.form_col{
    width: 80%;
    ::v-deep .el-form {
      width: 100%;
    }
    ::v-deep .el-form-item {
      width: 20%;
    }
  }
  .form_query{
    width: 20%;
    text-align: right;
  }
</style>
